package com.youlai.boot.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.youlai.boot.common.exception.BusinessException;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.system.mapper.UserMapper;
import com.youlai.boot.system.model.entity.User;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.SysEventCenterMapper;
import com.youlai.boot.system.service.SysEventCenterService;
import com.youlai.boot.system.model.entity.SysEventCenter;
import com.youlai.boot.system.model.form.SysEventCenterForm;
import com.youlai.boot.system.model.query.SysEventCenterQuery;
import com.youlai.boot.system.model.vo.SysEventCenterVO;
import com.youlai.boot.system.converter.SysEventCenterConverter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 赛事中心服务实现类
 *
 * @author lwb
 * @since 2025-03-22 18:29
 */
@Service
@RequiredArgsConstructor
public class SysEventCenterServiceImpl extends ServiceImpl<SysEventCenterMapper, SysEventCenter> implements SysEventCenterService {

    private final SysEventCenterConverter sysEventCenterConverter;

    @Resource
    private UserMapper userMapper;

    /**
     * 获取赛事中心分页列表
     *
     * @param pageReq 查询参数
     * @return {@link IPage<SysEventCenterVO>} 赛事中心分页列表
     */
    @Override
    public IPage<SysEventCenterVO> getSysEventCenterPage(SysEventCenterQuery pageReq) {
        Long userId = SecurityUtils.getUserId();
        User user = userMapper.selectById(userId);
        Assert.notNull(user, "用户不存在");
        if (user.getLicence().isBefore(LocalDate.now())) {
            throw new BusinessException("您的会员有效期已过，请联系管理员续费！");
        }
        //判断用户角色
//        Set<String> roles = SecurityUtils.getRoles();
//        if (roles.contains("GUEST")) {
//            pageReq.setCreateDate(LocalDate.now());
//        }

        if (CollUtil.isNotEmpty(pageReq.getStartDate()) && pageReq.getStartDate().size() == 2) {
            LocalDate start = pageReq.getStartDate().get(0);
            LocalDate end = pageReq.getStartDate().get(1);

            pageReq.setStartTime(start.atStartOfDay());
            pageReq.setEndTime(end.atTime(23, 59, 59));
        }

        Page<SysEventCenterVO> pageVO = this.baseMapper.getSysEventCenterPage(
                new Page<>(pageReq.getPageNum(), pageReq.getPageSize()),
                pageReq
        );
        return pageVO;
    }

    /**
     * 获取赛事中心表单数据
     *
     * @param id 赛事中心ID
     * @return
     */
    @Override
    public SysEventCenterForm getSysEventCenterFormData(Long id) {
        SysEventCenter entity = this.getById(id);
        return sysEventCenterConverter.toForm(entity);
    }

    /**
     * 新增赛事中心
     *
     * @param formData 赛事中心表单对象
     * @return
     */
    @Override
    public boolean saveSysEventCenter(SysEventCenterForm formData) {
        SysEventCenter entity = sysEventCenterConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新赛事中心
     *
     * @param id       赛事中心ID
     * @param formData 赛事中心表单对象
     * @return
     */
    @Override
    public boolean updateSysEventCenter(Long id, SysEventCenterForm formData) {
        SysEventCenter entity = sysEventCenterConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除赛事中心
     *
     * @param ids 赛事中心ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSysEventCenters(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的赛事中心数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
